package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.LinLiQuanAdapter;
import com.zdsoft.blp.zongbuju.shouye.wuye.entity.WuYe;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/8/30.
 */
public class LinLiQuanActivity extends BaseActivity implements NetCallback{
    private TextView tv_title;
    private GridView linli_gv;
    private List<WuYe>list;
    private WuYe wuYe;
    private String[]ziti={"社区论坛","拼车信息","二手市场","自助旅游","求职招聘","寻物招领","房屋租售"};
    private int[]tupian={R.drawable.aon,R.drawable.aon,R.drawable.aon,R.drawable.aon,R.drawable.aon,R.drawable.aon,R.drawable.aon};
    @Override
    protected void init() {
        setContentView(R.layout.linliquan);
        linli_gv=this.find(R.id.linli_gv);
        tv_title = find(R.id.tv_title);
        tv_title.setText("邻里圈");

        Net net=new Net();
        net.url= API.BMINFO_URL;
        net.nameSpace=API.BMINFO_NAMESPACE;
        net.methodName=API.COLUMNLIST ;
        Map<String,Object> map=new HashMap<>();
        map.put("communityCode","00001");
        net.parms=map;
        getServer(net,this);


        linli_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=null;
                switch (position){
                    case 0:
                        intent=new Intent(LinLiQuanActivity.this,LLLunTanActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent=new Intent(LinLiQuanActivity.this,LLPinCheActivitv.class);
                        startActivity(intent);
                        break;
                    case 2:
                        intent=new Intent(LinLiQuanActivity.this,LLShiChangActivitv.class);
                        startActivity(intent);
                        break;
                    case 3:
                        intent=new Intent(LinLiQuanActivity.this,LLLvYouActivity.class);
                        intent.putExtra("title","自助旅游");
                        startActivity(intent);
                        break;
                    case 4:
                        intent=new Intent(LinLiQuanActivity.this,LLZhaoPinActivity.class);
                        startActivity(intent);
                        break;
                    case 5:
                        intent=new Intent(LinLiQuanActivity.this,LLXunWuActivity.class);
                        startActivity(intent);
                        break;
                    case 6:
                        intent=new Intent(LinLiQuanActivity.this,LLZuLinActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("asd",res);
          list=jiexi(res);
        LinLiQuanAdapter adapter=new LinLiQuanAdapter(list,this);
          linli_gv.setAdapter(adapter);


    }

    private List<WuYe> jiexi(String res) {
        list=new ArrayList<>();
        try {
            JSONArray array=new JSONArray(res);
            for(int i=0;i<array.length();i++){
                JSONObject object=array.getJSONObject(i);
                String id=object.getString("id");
                String name=object.getString("name");
                JSONArray array1=object.getJSONArray("children");
                if ("邻里圈".equals(name)){
                    for (int j=0;j<array1.length();j++){
                        JSONObject object1=array1.getJSONObject(j);
                        String id1=object1.getString("id");
                        String name1=object1.getString("name");
                        String imgPath=object1.getString("bmColumnImgPath");
                        wuYe=new WuYe(id1,name1,imgPath);
                        list.add(wuYe);

                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
